package org.leetcode.easy.leetcode242;

import java.util.HashMap;

public class Solution
{
    public boolean isAnagram(String s, String t) {


        if (s.length()!=t.length())
        {
            return false;
        }

        HashMap<String, Integer> sMap = new HashMap<>();
        HashMap<String, Integer> tMap = new HashMap<>();


        for (String s1 : s.split("")) {
            if (sMap.containsKey(s1)) {
                Integer time = sMap.get(s1);
                time=time+1;
                sMap.put(s1,time);
            }else {
                sMap.put(s1,1);
            }
        }

        for (String t1 : t.split("")) {
            if (tMap.containsKey(t1)) {
                Integer time = tMap.get(t1);
                time=time+1;
                tMap.put(t1,time);
            }else {
                tMap.put(t1,1);
            }
        }


        for (String s1:sMap.keySet()) {
            if (tMap.get(s1)!=sMap.get(s1))
            {
                return false;
            }
        }
        return true;
    }




    public boolean isAnagram2(String s ,String t)
    {

        if (s.length()!=t.length())
        {
            return false;
        }

        int [] record = new int[26];

        for (int i = 0; i < s.length(); i++) {
            record[s.charAt(i)-'a']++;
        }

        for (int i = 0; i < t.length(); i++) {
            record[t.charAt(i)-'a']--;
        }

        for (int i = 0; i < record.length; i++) {
            if (record[i]!=0)
            {
                return false;
            }
        }

        return true;



    }
    public static void main(String[] args) {
        String s1= "rat";
        String t1= "car";

        Solution solution = new Solution();
        boolean isSame = solution.isAnagram(s1, t1);

        System.out.println(isSame);
    }
}
